/**
 * Created by Administrator on 2017/08/16 .
 */

'use strict';
var React = require('react-native');
import net from 'net';
import  ProtobufJsManager from "../proto/ProtobufJsManager"


export default {

    getTimeStampHex(){
        console.log("timestamp", parseInt(new Date().getTime() / 1000))
        return (parseInt(new Date().getTime() / 1000)).toString(16)
    },

    SnapConfig(ssid, wifipswd) {

        return new Promise((fullfill, reject) => {
            if (ssid != "" && wifipswd != "") {

                let SnapMsg = ProtobufJsManager.getProtoMessage(ProtobufJsManager.headerType.SnapApp);
                //根据实际情况set
                let snapconfig = {SSID: ssid, password: wifipswd}

                let loopcount = ProtobufJsManager.getLoopCount();

                let data = {
                    loopCount: loopcount,
                    timestamp: this.getTimeStampHex(),
                    type: 4,
                    config: snapconfig
                };


                console.log('SnapConfig DATA', data);
                let message = SnapMsg.create(data);


                //生成一个SnapMsg消息
                ProtobufJsManager.createMessage(message, ProtobufJsManager.headerType.SnapApp).then((createMsg) => {
                    //生成的消息
                    console.log("create message success:" + createMsg);


                    // ProtobufJsManager.parseMessage(createMsg, ProtobufJsManager.headerType.SnapApp).then((result) => {
                    //     console.log('GET DATA RESULT FROM DEVICE', result);
                    // }, (error) => {
                    //     console.log("error=" + error)
                    // });
                    // this.tcpsend = createMsg
                    // this.reload()

                    fullfill({"loopcount": loopcount, "msg": createMsg})
                }, (error) => {

                    console.log("create message fail:" + error)
                    reject(error)
                });
            } else {
                //  alert("wifi or pswd blank")

                reject("wifi or pswd blank")
            }
        })
    },

    start(time){
        return new Promise((fullfill, reject) => {
            if (time <= 11 && time > 0) {
                let SnapMsg = ProtobufJsManager.getProtoMessage(ProtobufJsManager.headerType.SnapApp);


                let loopcount = ProtobufJsManager.getLoopCount();

                let data = {
                    loopCount: loopcount,
                    timestamp: this.getTimeStampHex(),
                    type: 5,
                    start: time
                };


                console.log('snap start DATA', data);
                let message = SnapMsg.create(data);


                //生成一个SnapMsg消息
                ProtobufJsManager.createMessage(message, ProtobufJsManager.headerType.SnapApp).then((createMsg) => {
                    //生成的消息
                    console.log("create message success:" + createMsg);


                    fullfill({"loopcount": loopcount, "msg": createMsg})
                }, (error) => {

                    console.log("create message fail:" + error)
                    reject(error)
                });
            } else {
                reject("time not between 1 and 11")
            }
        })
    },
    getDataList(){

        return new Promise((fulfill, reject) => {
            let SnapMsg = ProtobufJsManager.getProtoMessage(ProtobufJsManager.headerType.SnapApp);


            let loopcount = ProtobufJsManager.getLoopCount();

            let data = {
                loopCount: loopcount,
                timestamp: this.getTimeStampHex(),
                type: 6,
            };


            console.log('snap getdata list', data);
            let message = SnapMsg.create(data);


            //生成一个SnapMsg消息
            ProtobufJsManager.createMessage(message, ProtobufJsManager.headerType.SnapApp).then((createMsg) => {
                //生成的消息
                console.log("create message success:" + createMsg);


                fulfill({"loopcount": loopcount, "msg": createMsg})
            }, (error) => {

                console.log("create message fail:" + error)
                reject(error)
            });

        })
    },
    getData(index){
        return new Promise((fulfill, reject) => {
            let SnapMsg = ProtobufJsManager.getProtoMessage(ProtobufJsManager.headerType.SnapApp);


            let loopcount = ProtobufJsManager.getLoopCount();

            let data = {
                loopCount: loopcount,
                timestamp: this.getTimeStampHex(),
                type: 7,
                queryData: index
            };


            console.log('snap getdata', data);
            let message = SnapMsg.create(data);


            //生成一个SnapMsg消息
            ProtobufJsManager.createMessage(message, ProtobufJsManager.headerType.SnapApp).then((createMsg) => {
                //生成的消息
                console.log("create message success:" + createMsg);


                fulfill({"loopcount": loopcount, "msg": createMsg})
            }, (error) => {

                console.log("create message fail:" + error)
                reject(error)
            });

        })
    },
    getDataEnv(index){
        return new Promise((fulfill, reject) => {
            let SnapMsg = ProtobufJsManager.getProtoMessage(ProtobufJsManager.headerType.SnapApp);


            let loopcount = ProtobufJsManager.getLoopCount();

            let data = {
                loopCount: loopcount,
                timestamp: this.getTimeStampHex(),
                type: 8,
                queryDataEnv: index
            };


            console.log('snap getdataenv', data);
            let message = SnapMsg.create(data);


            //生成一个SnapMsg消息
            ProtobufJsManager.createMessage(message, ProtobufJsManager.headerType.SnapApp).then((createMsg) => {
                //生成的消息
                console.log("create message success:" + createMsg);


                fulfill({"loopcount": loopcount, "msg": createMsg})
            }, (error) => {

                console.log("create message fail:" + error)
                reject(error)
            });

        })
    },
    getWavList(index){
        return new Promise((fulfill, reject) => {
            let SnapMsg = ProtobufJsManager.getProtoMessage(ProtobufJsManager.headerType.SnapApp);


            let loopcount = ProtobufJsManager.getLoopCount();

            let data = {
                loopCount: loopcount,
                timestamp: this.getTimeStampHex(),
                type: 9,
                queryAudioList: index
            };


            console.log('snap get wav list', data);
            let message = SnapMsg.create(data);


            //生成一个SnapMsg消息
            ProtobufJsManager.createMessage(message, ProtobufJsManager.headerType.SnapApp).then((createMsg) => {
                //生成的消息
                console.log("create message success:" + createMsg);


                fulfill({"loopcount": loopcount, "msg": createMsg})
            }, (error) => {

                console.log("create message fail:" + error)
                reject(error)
            });

        })
    },

    getWav(index){
        return new Promise((fulfill, reject) => {
            let SnapMsg = ProtobufJsManager.getProtoMessage(ProtobufJsManager.headerType.SnapApp);


            let loopcount = ProtobufJsManager.getLoopCount();

            let data = {
                loopCount: loopcount,
                timestamp: this.getTimeStampHex(),
                type: 10,
                queryAudioWav: index
            };


            console.log('snap get wav', data);
            let message = SnapMsg.create(data);


            //生成一个SnapMsg消息
            ProtobufJsManager.createMessage(message, ProtobufJsManager.headerType.SnapApp).then((createMsg) => {
                //生成的消息
                console.log("create message success:" + createMsg);


                fulfill({"loopcount": loopcount, "msg": createMsg})
            }, (error) => {

                console.log("create message fail:" + error)
                reject(error)
            });

        })
    }


}
